home *** CD-ROM | disk | FTP | other *** search
/ Mac Mania 5 / MacMania 5.toast / / Tools&Utilities / Password Dialog OSAX / PassWord.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-09-12  |  3.4 KB  |  138 lines  |  [TEXT/KAHL]

  1. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2. //
  3. //    Password.c
  4. //    Written by: Donald Olson
  5. //    May 1993
  6. //    Copyright ® 1993 Apple Computer Inc.
  7. //     All rights reserved.
  8. //
  9. //    This is a simple Scripting Addition that asks the user to type in a
  10. //    password.  This was written for a talk at the 1993 WWDC given
  11. //     by Donald Olson and Donn Denman.
  12. //
  13. //  Modified 96.09.12 by Philippe Casgrain <casgrain@umich.edu> 
  14. //  to return the password in clear text
  15. //
  16. //    Syntax: User Password
  17. //    Return: Text of password or cancel.
  18. //
  19. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  20.  
  21.  
  22. #include <string.h>
  23. #include <Dialogs.h>
  24. #include <AppleEvents.h>
  25. //AEVTOLIEpswd
  26.  
  27. // Our headers
  28. void        FrameDefault( DialogPtr theDialog, long theItem);
  29. pascal     Boolean MyDlgFilter(    DialogPtr theDlg, 
  30.                             EventRecord *theEventRec, short *itemHit);
  31. void     EncryptString(StringPtr strPtr);
  32.  
  33. // Our constant
  34. #define DLOGID 128
  35.  
  36. pascal OSErr main(AppleEvent *theEvent, 
  37.                 AppleEvent *theReply, 
  38.                 long theRefCon) 
  39. {
  40.     
  41.     DialogPtr         theDialog = nil;
  42.     OSErr            theErr = noErr;
  43.     GrafPtr            savedPort;
  44.     short            itemHit;
  45.     Str32            theString;
  46.     StringPtr        theStrPtr = (StringPtr)&theString;
  47.     StringHandle    theStrHdl = (StringHandle)&theStrPtr;
  48.     
  49.     GetPort(&savedPort);
  50.     
  51.     theErr = AEInteractWithUser(kAEDefaultTimeout, nil, nil);
  52.     if(theErr != noErr) return theErr;
  53.     
  54.     theDialog = GetNewDialog(DLOGID, nil, (WindowPtr)-1);    
  55.         
  56.         if(theDialog == nil) return resNotFound;        
  57.        
  58.         SetPort(theDialog);
  59.     FrameDefault(theDialog, ok);
  60.     
  61.     strcpy((char*)theString, (char*) "\0");         // Empty string
  62.     
  63.     /*     
  64.         Use window refCon to pass user input back to this routine
  65.         since the text field will have our bullets instead of the
  66.         actual text entered.
  67.     */
  68.     
  69.     SetWRefCon(theDialog, (long)theStrHdl);    
  70.     
  71.     do {
  72.         ModalDialog((ModalFilterProcPtr)MyDlgFilter, &itemHit);
  73.     } while (itemHit != cancel && itemHit != ok);
  74.      
  75.      if(itemHit == cancel) {
  76.          DisposDialog(theDialog);
  77.          return userCanceledErr;
  78.      }
  79.     
  80.     DisposDialog(theDialog);
  81.     
  82.     //Commented by PhC to return clear text password
  83.     //EncryptString(theStrPtr);
  84.     
  85.     theErr = AEPutParamPtr(theReply, keyDirectObject, typeChar, 
  86.                             (Ptr)theString, strlen((char*)theString));
  87.     SetPort(savedPort);
  88.     return theErr;
  89. }
  90.  
  91. void EncryptString(StringPtr strPtr) {
  92.     short    theSize, x;
  93.     theSize = strlen((char*)(strPtr));
  94.     
  95.     for( x = 0; x <= (theSize -1); x++) {
  96.         strPtr[x] = (short)strPtr[x] + (short)"\¡";
  97.     }
  98. }
  99.  
  100. pascal Boolean MyDlgFilter(DialogPtr theDlg, EventRecord *theEventRec, short *itemHit) {    
  101.     
  102.     StringHandle    theStrHdl;
  103.     short            theSize;
  104.     if(theEventRec->what != keyDown) // Just looking for keystrokes
  105.         return false;
  106.             
  107.     switch((theEventRec->message) & charCodeMask)
  108.     {
  109.         case 0x0d:                // Return
  110.             *itemHit = ok;      
  111.             return true;
  112.         case 0x03:                // Enter
  113.             *itemHit = ok;      
  114.             return true;
  115.         case 0x08:                // Backspace
  116.             return false;
  117.         default:
  118.             theStrHdl = (StringHandle)GetWRefCon(theDlg);
  119.             theSize = strlen((char*)(*theStrHdl));
  120.             (*theStrHdl)[theSize] = (char)(theEventRec->message);
  121.             (*theStrHdl)[theSize + 1] = (char)NULL;
  122.             theEventRec->message = '•';
  123.             return false;
  124.     }
  125. }
  126.  
  127. void FrameDefault( DialogPtr theDialog, long theItem) {
  128.     short    DType; 
  129.     Handle    DItem;
  130.     Rect    DRect;
  131.     
  132.     GetDItem(theDialog, theItem, &DType, &DItem, &DRect); 
  133.     PenSize(3, 3); 
  134.     InsetRect(&DRect, -4, -4);
  135.     FrameRoundRect(&DRect, 16, 16);
  136.     PenSize(1, 1);
  137. }
  138.